﻿using System;
using System.Windows.Forms;

//自定义命名空间
using DreamCube.Foundation.DllImport;
using DreamCube.Foundation.Basic.Utility;

namespace BGN.PlugIn.Client.Msg
{
    public partial class ShowMsgForm : Form
    {
        private String msgFilePath = String.Empty;
        private String userName = String.Empty; //用户的姓名
        private String userPassword = String.Empty;
        private String msgID = String.Empty;
        private String userLoginName = String.Empty; //用户的登录名【用户名】

        public ShowMsgForm(String msgID, String msgFilePath, String userName, String userLoginName, String password)
        {
            InitializeComponent();
            webBrowser1.ScriptErrorsSuppressed = true;  //禁止脚本错误弹出框
            this.msgFilePath = msgFilePath;
            this.userName = userName;
            this.userPassword = password;
            this.msgID = msgID;
            this.userLoginName = userLoginName;
        }

        /// <summary>
        /// 窗体加载的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShowMsgForm_Load(Object sender, EventArgs e)
        {
            //this.linkLabel1.Text = this.msg;
            webBrowser1.Navigate(msgFilePath);
            webBrowser1.Document.MouseDown +=new HtmlElementEventHandler(Document_MouseDown);

            int x = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width - this.Width - 10;
            int y = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height - this.Height - 10;
            this.SetDesktopLocation(x, y);//窗体弹出的位置
            Win32.SetWindowPos(this.Handle, 100, Screen.PrimaryScreen.Bounds.Width - this.Width, Screen.PrimaryScreen.Bounds.Height - this.Height - 30, 50, 50, 5);//调用WindowsAPI
            Win32.AnimateWindow(this.Handle, 1000, Win32.AW_VER_NEGATIVE);//调用WindowsAPI  
        }

        void Document_MouseDown(Object sender, HtmlElementEventArgs e)
        {
            try
            {
                HtmlElement element = webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition);
                if (element == null || element.TagName != "A") return;
                String html = element.OuterHtml;
                String[] clickEvent = MyString.SubStringBetweenStr(html, "onclick='", "'", false);
                if (clickEvent != null && clickEvent.Length > 0)
                {
                    IEWinHelper.SendCommandToOASystem(this.userLoginName, this.userName, this.userPassword, clickEvent[0], null);
                    //标记已读并且关闭窗口
                    //必须传入的是用户名
                    MsgHelper.AddReader(this.msgID, this.userName);
                    //this.Close();
                }
            }
            catch (Exception ex)
            {
                DreamCube.Foundation.Basic.Utility.MyLog.MakeLog(ex);
            }
        }

        /// <summary>
        /// 标志已读按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //必须传入的是用户名
            MsgHelper.AddReader(this.msgID, this.userName);
        }
    }
}
